package listbyorder.access201_300.test347;

import java.util.*;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/1 21:41
 */
public class Solution1 {

    public int[] topKFrequent(int[] nums, int k) {
        int[] res = new int[k];
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            int c = map.getOrDefault(num, 0);
            map.put(num, c + 1);
        }
        PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o2[1] - o1[1];
            }
        });
        for (Integer num : map.keySet()) {
            queue.add(new int[]{num, map.get(num)});
        }
        int index = 0;
        while (index < k) {
            res[index++] = queue.poll()[0];
        }
        return res;
    }
}
